<!DOCTYPE HTML>
<html>

<head>
	<link rel="bookmark"  type="image/x-icon"  href="/img/logo_miccall.png"/>
	<link rel="shortcut icon" href="/img/logo_miccall.png">
	
			    <title>
    Ame雨
    </title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <link rel="stylesheet" href="/amelin-ting/css/mic_main.css" />
    <link rel="stylesheet" href="/amelin-ting/css/dropdownMenu.css" />
    <meta name="keywords" content="Ame雨" />
    
    	<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
	 
    <noscript>
        <link rel="stylesheet" href="/amelin-ting/css/noscript.css" />
    </noscript>
    <style type="text/css">
        body:before {
          content: ' ';
          position: fixed;
          top: 0;
          background: url('/amelin-ting/img/bg.jpg') center 0 no-repeat;
          right: 0;
          bottom: 0;
          left: 0;
          background-size: cover; 
        }
    </style>

			    
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script async type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


    <script src="/amelin-ting/js/jquery.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrollex.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrolly.min.js"></script>
    <script src="/amelin-ting/js/skel.min.js"></script>
    <script src="/amelin-ting/js/util.js"></script>
    <script src="/amelin-ting/js/main.js"></script>
	
<meta name="generator" content="Hexo 5.4.0"></head>
    
		
<!-- Layouts -->



<!--  代码渲染  -->
<link rel="stylesheet" href="/css/prism_coy.css" />
<link rel="stylesheet" href="/css/typo.css" />
<!-- 文章页 -->
<body class="is-loading">
    <!-- Wrapper 外包 s-->
    <div id="wrapper" class="fade-in">
        <!-- Intro 头部显示 s -->
        <!-- Intro 头部显示 e -->
        <!-- Header 头部logo start -->
        <header id="header">
    <a href="/amelin-ting/" class="logo">Art Space</a>
</header>
        <!-- Nav 导航条 start -->
        <nav id="nav" class="special" >
            <ul class="menu links" >
			<!-- Homepage  主页  --> 
			<li >
	            <a href="/amelin-ting/" rel="nofollow">主页</a>
	        </li>
			<!-- categories_name  分类   --> 
	        
	        <li class="active">
	            <a href="#s1">分类</a>
	                    <ul class="submenu">
	                        <li>
	                        <a class="category-link" href="/amelin-ting/categories/Go/">Go</a></li><li><a class="category-link" href="/amelin-ting/categories/django/">django</a></li><li><a class="category-link" href="/amelin-ting/categories/drf/">drf</a></li><li><a class="category-link" href="/amelin-ting/categories/gin/">gin</a></li><li><a class="category-link" href="/amelin-ting/categories/go/">go</a></li><li><a class="category-link" href="/amelin-ting/categories/linux/">linux</a></li><li><a class="category-link" href="/amelin-ting/categories/mysql/">mysql</a></li><li><a class="category-link" href="/amelin-ting/categories/python/">python</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F/">内网穿透</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%89%8D%E7%AB%AF/">前端</a>
	                    </ul>
	        </li>
	        
	        <!-- archives  归档   --> 
	        
	        
		        <!-- Pages 自定义   -->
		        
		        <li>
		            <a href="/about/" title="简历">
		                简历
		            </a>
		        </li>
		        
		        <li>
		            <a href="/group/" title="团队">
		                团队
		            </a>
		        </li>
		        
		        <li>
		            <a href="/gallery/" title="图库">
		                图库
		            </a>
		        </li>
		        
		        <li>
		            <a href="/tag/" title="标签">
		                标签
		            </a>
		        </li>
		        


            </ul>
            <!-- icons 图标   -->
			<ul class="icons">
                    
                    <li>
                        <a title="github" href="https://github.com/miccall" target="_blank" rel="noopener">
                            <i class="icon fa fa-github"></i>
                        </a>
                    </li>
                    
                    <li>
                        <a title="500px" href="http://500px.com" target="_blank" rel="noopener">
                            <i class="icon fa fa-500px"></i>
                        </a>
                    </li>
                    
			</ul>
</nav>

        <div id="main" >
            <div class ="post_page_title_img" style="height: 25rem;background-image: url(https://img1.baidu.com/it/u=115223947,2740893225&amp;fm=26&amp;fmt=auto);background-position: center; background-repeat:no-repeat; background-size:cover;-moz-background-size:cover;overflow:hidden;" >
                <a href="#" style="padding: 4rem 4rem 2rem 4rem ;"><h2 >djangoForm组件</h2></a>
            </div>
            <!-- Post -->
            <div class="typo" style="padding: 3rem;">
                <h2 id="一、forms表单前戏"><a href="#一、forms表单前戏" class="headerlink" title="一、forms表单前戏"></a>一、forms表单前戏</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">写一个注册功能</span></span><br><span class="line"><span class="string">	获取用户名和密码 利用form表单提交数据</span></span><br><span class="line"><span class="string">	在后端判断用户名和密码是否符合一定的条件</span></span><br><span class="line"><span class="string">		用户名中不能含有金瓶梅</span></span><br><span class="line"><span class="string">		密码不能少于三位</span></span><br><span class="line"><span class="string">	</span></span><br><span class="line"><span class="string">	如何符合条件需要你将提示信息展示到前端页面</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># views</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">ab_form</span>(<span class="params">request</span>):</span></span><br><span class="line">    back_dic = &#123;<span class="string">&#x27;username&#x27;</span>:<span class="string">&#x27;&#x27;</span>,<span class="string">&#x27;password&#x27;</span>:<span class="string">&#x27;&#x27;</span>&#125;</span><br><span class="line">    <span class="keyword">if</span> request.method == <span class="string">&#x27;POST&#x27;</span>:</span><br><span class="line">        username = request.POST.get(<span class="string">&#x27;username&#x27;</span>)</span><br><span class="line">        password = request.POST.get(<span class="string">&#x27;password&#x27;</span>)</span><br><span class="line">        <span class="keyword">if</span> <span class="string">&#x27;金瓶梅&#x27;</span> <span class="keyword">in</span> username:</span><br><span class="line">            back_dic[<span class="string">&#x27;username&#x27;</span>] = <span class="string">&#x27;不符合社会主义核心价值观&#x27;</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">len</span>(password) &lt; <span class="number">3</span>:</span><br><span class="line">            back_dic[<span class="string">&#x27;password&#x27;</span>] = <span class="string">&#x27;不能太短 不好!&#x27;</span></span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    无论是post请求还是get请求</span></span><br><span class="line"><span class="string">    页面都能够获取到字典 只不过get请求来的时候 字典值都是空的</span></span><br><span class="line"><span class="string">    而post请求来之后 字典可能有值</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="keyword">return</span> render(request,<span class="string">&#x27;ab_form.html&#x27;</span>,<span class="built_in">locals</span>())</span><br><span class="line"></span><br><span class="line"><span class="comment"># html</span></span><br><span class="line">&lt;form action=<span class="string">&quot;&quot;</span> method=<span class="string">&quot;post&quot;</span>&gt;</span><br><span class="line">    &lt;p&gt;username:</span><br><span class="line">        &lt;<span class="built_in">input</span> <span class="built_in">type</span>=<span class="string">&quot;text&quot;</span> name=<span class="string">&quot;username&quot;</span>&gt;</span><br><span class="line">        &lt;span style=<span class="string">&quot;color: red&quot;</span>&gt;&#123;&#123; back_dic.username &#125;&#125;&lt;/span&gt;</span><br><span class="line">    &lt;/p&gt;</span><br><span class="line">    &lt;p&gt;password:</span><br><span class="line">        &lt;<span class="built_in">input</span> <span class="built_in">type</span>=<span class="string">&quot;text&quot;</span> name=<span class="string">&quot;password&quot;</span>&gt;</span><br><span class="line">        &lt;span style=<span class="string">&quot;color: red&quot;</span>&gt;&#123;&#123; back_dic.password &#125;&#125;&lt;/span&gt;</span><br><span class="line">    &lt;/p&gt;</span><br><span class="line">    &lt;<span class="built_in">input</span> <span class="built_in">type</span>=<span class="string">&quot;submit&quot;</span> <span class="class"><span class="keyword">class</span>=&quot;<span class="title">btn</span> <span class="title">btn</span>-<span class="title">info</span>&quot;&gt;</span></span><br><span class="line"><span class="class">&lt;/<span class="title">form</span>&gt;</span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class">&quot;&quot;&quot;</span></span><br><span class="line"><span class="class">1.手动书写前端获取用户数据的<span class="title">html</span>代码						渲染<span class="title">html</span>代码</span></span><br><span class="line"><span class="class">2.后端对用户数据进行校验											 校验数据</span></span><br><span class="line"><span class="class">3.对不符合要求的数据进行前端提示								展示提示信息</span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"><span class="title">forms</span>组件</span></span><br><span class="line"><span class="class">	能够完成的事情</span></span><br><span class="line"><span class="class">			1.渲染<span class="title">html</span>代码</span></span><br><span class="line"><span class="class">			2.校验数据</span></span><br><span class="line"><span class="class">			3.展示提示信息</span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class">为什么数据校验非要去后端 不能在前端利用<span class="title">js</span>直接完成呢？</span></span><br><span class="line"><span class="class">	数据校验前端可有可无</span></span><br><span class="line"><span class="class">	但是后端必须要有!!!</span></span><br><span class="line"><span class="class">	</span></span><br><span class="line"><span class="class">	因为前端的校验是弱不禁风的 你可以直接修改</span></span><br><span class="line"><span class="class">	或者利用爬虫程序绕过前端页面直接朝后端提交数据</span></span><br><span class="line"><span class="class">	</span></span><br><span class="line"><span class="class">	购物网站	</span></span><br><span class="line"><span class="class">		选取了货物之后 会计算一个价格发送给后端 如果后端不做价格的校验</span></span><br><span class="line"><span class="class">		</span></span><br><span class="line"><span class="class">		实际是获取到用户选择的所有商品的主键值</span></span><br><span class="line"><span class="class">		然后在后端查询出所有商品的价格 再次计算一遍</span></span><br><span class="line"><span class="class">		如果跟前端一致 那么完成支付如果不一致直接拒绝</span></span><br><span class="line"><span class="class">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>

<h2 id="二、forms组件类书写"><a href="#二、forms组件类书写" class="headerlink" title="二、forms组件类书写"></a>二、forms组件类书写</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> django <span class="keyword">import</span> forms</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyForm</span>(<span class="params">forms.Form</span>):</span></span><br><span class="line">    <span class="comment"># 名字必须最小长度为3 最大长度为8</span></span><br><span class="line">    name = forms.CharField(min_length=<span class="number">3</span>, max_length=<span class="number">8</span>)</span><br><span class="line">    <span class="comment"># 密码也是一样</span></span><br><span class="line">    password = forms.CharField(min_length=<span class="number">3</span>, max_length=<span class="number">8</span>)</span><br><span class="line">    <span class="comment"># 邮箱必须符合邮箱格式 @xx.com</span></span><br><span class="line">    emali = forms.EmailField()</span><br></pre></td></tr></table></figure>

<h2 id="三、forms组件校验数据"><a href="#三、forms组件校验数据" class="headerlink" title="三、forms组件校验数据"></a>三、forms组件校验数据</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">1.测试环境的准备 可以自己拷贝代码准备</span></span><br><span class="line"><span class="string">2.其实在pycharm里面已经帮你准备一个测试环境</span></span><br><span class="line"><span class="string">	python console</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">    <span class="keyword">from</span> app01 <span class="keyword">import</span> views</span><br><span class="line">    <span class="comment"># 1 将带校验的数据组织成字典的形式传入即可</span></span><br><span class="line">    form_obj = views.MyForm(&#123;<span class="string">&#x27;username&#x27;</span>:<span class="string">&#x27;jason&#x27;</span>,<span class="string">&#x27;password&#x27;</span>:<span class="string">&#x27;123&#x27;</span>,<span class="string">&#x27;email&#x27;</span>:<span class="string">&#x27;123&#x27;</span>&#125;)</span><br><span class="line">    <span class="comment"># 2 判断数据是否合法		注意该方法只有在所有的数据全部合法的情况下才会返回True</span></span><br><span class="line">    form_obj.is_valid()</span><br><span class="line">    <span class="literal">False</span></span><br><span class="line">    <span class="comment"># 3 查看所有校验通过的数据</span></span><br><span class="line">    form_obj.cleaned_data</span><br><span class="line">    &#123;<span class="string">&#x27;username&#x27;</span>: <span class="string">&#x27;jason&#x27;</span>, <span class="string">&#x27;password&#x27;</span>: <span class="string">&#x27;123&#x27;</span>&#125;</span><br><span class="line">    <span class="comment"># 4 查看所有不符合校验规则以及不符合的原因</span></span><br><span class="line">    form_obj.errors</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="string">&#x27;email&#x27;</span>: [<span class="string">&#x27;Enter a valid email address.&#x27;</span>]</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment"># 5 校验数据只校验类中出现的字段 多传不影响 多传的字段直接忽略</span></span><br><span class="line">    form_obj = views.MyForm(&#123;<span class="string">&#x27;username&#x27;</span>:<span class="string">&#x27;jason&#x27;</span>,<span class="string">&#x27;password&#x27;</span>:<span class="string">&#x27;123&#x27;</span>,<span class="string">&#x27;email&#x27;</span>:<span class="string">&#x27;123@qq.com&#x27;</span>,<span class="string">&#x27;hobby&#x27;</span>:<span class="string">&#x27;study&#x27;</span>&#125;)</span><br><span class="line">    form_obj.is_valid()</span><br><span class="line">    <span class="literal">True</span></span><br><span class="line">    <span class="comment"># 6 校验数据 默认情况下 类里面所有的字段都必须传值</span></span><br><span class="line">    form_obj = views.MyForm(&#123;<span class="string">&#x27;username&#x27;</span>:<span class="string">&#x27;jason&#x27;</span>,<span class="string">&#x27;password&#x27;</span>:<span class="string">&#x27;123&#x27;</span>&#125;)</span><br><span class="line">    form_obj.is_valid()</span><br><span class="line">    <span class="literal">False</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">也就意味着校验数据的时候 默认情况下数据可以多传但是绝不可能少传</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>

<h2 id="四、forms组件渲染标签"><a href="#四、forms组件渲染标签" class="headerlink" title="四、forms组件渲染标签"></a>四、forms组件渲染标签</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">forms组件只会自动帮你渲染获取用户输入的标签(input select radio checkbox)</span></span><br><span class="line"><span class="string">不能帮你渲染提交按钮</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">	<span class="comment"># views</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">index</span>(<span class="params">request</span>):</span></span><br><span class="line">        <span class="comment"># 1 先产生一个空对象</span></span><br><span class="line">        form_obj = MyForm()</span><br><span class="line">        <span class="comment"># 2 直接将该空对象传递给html页面</span></span><br><span class="line">        <span class="keyword">return</span> render(request,<span class="string">&#x27;index.html&#x27;</span>,<span class="built_in">locals</span>())</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 前端利用空对象做操作</span></span><br><span class="line">        &lt;p&gt;第一种渲染方式:代码书写极少，封装程度太高 不便于后续的扩展 一般情况下只在本地测试使用&lt;/p&gt;</span><br><span class="line">        &#123;&#123; form_obj.as_p &#125;&#125;</span><br><span class="line">        &#123;&#123; form_obj.as_ul &#125;&#125;</span><br><span class="line">        &#123;&#123; form_obj.as_table &#125;&#125;</span><br><span class="line">        &lt;p&gt;第二种渲染方式:可扩展性很强 但是需要书写的代码太多  一般情况下不用&lt;/p&gt;</span><br><span class="line">        &lt;p&gt;&#123;&#123; form_obj.username.label &#125;&#125;:&#123;&#123; form_obj.username &#125;&#125;&lt;/p&gt;</span><br><span class="line">        &lt;p&gt;&#123;&#123; form_obj.password.label &#125;&#125;:&#123;&#123; form_obj.password &#125;&#125;&lt;/p&gt;</span><br><span class="line">        &lt;p&gt;&#123;&#123; form_obj.email.label &#125;&#125;:&#123;&#123; form_obj.email &#125;&#125;&lt;/p&gt;</span><br><span class="line">        &lt;p&gt;第三种渲染方式(推荐使用):代码书写简单 并且扩展性也高&lt;/p&gt;</span><br><span class="line">        &#123;% <span class="keyword">for</span> form <span class="keyword">in</span> form_obj %&#125;</span><br><span class="line">            &lt;p&gt;&#123;&#123; form.label &#125;&#125;:&#123;&#123; form &#125;&#125;&lt;/p&gt;</span><br><span class="line">        &#123;% endfor %&#125;</span><br><span class="line"> </span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">label属性默认展示的是类中定义的字段首字母大写的形式</span></span><br><span class="line"><span class="string">也可以自己修改 直接给字段对象加label属性即可</span></span><br><span class="line"><span class="string">	 username = forms.CharField(min_length=3,max_length=8,label=&#x27;用户名&#x27;)</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>

<h2 id="五、forms组件渲染错误信息"><a href="#五、forms组件渲染错误信息" class="headerlink" title="五、forms组件渲染错误信息"></a>五、forms组件渲染错误信息</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">浏览器会自动帮你校验数据 但是前端的校验弱不禁风</span></span><br><span class="line"><span class="string">如何让浏览器不做校验</span></span><br><span class="line"><span class="string">	&lt;form action=&quot;&quot; method=&quot;post&quot; novalidate&gt;</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">	<span class="comment"># views</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">index</span>(<span class="params">request</span>):</span></span><br><span class="line">        <span class="comment"># 1 先产生一个空对象</span></span><br><span class="line">        form_obj = MyForm()</span><br><span class="line">        <span class="keyword">if</span> request.method == <span class="string">&#x27;POST&#x27;</span>:</span><br><span class="line">            <span class="comment"># 获取用户数据并且校验</span></span><br><span class="line">            <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">            1.数据获取繁琐</span></span><br><span class="line"><span class="string">            2.校验数据需要构造成字典的格式传入才行</span></span><br><span class="line"><span class="string">            ps:但是request.POST可以看成就是一个字典</span></span><br><span class="line"><span class="string">            &quot;&quot;&quot;</span></span><br><span class="line">            <span class="comment"># 3.校验数据</span></span><br><span class="line">            form_obj = MyForm(request.POST)</span><br><span class="line">            <span class="comment"># 4.判断数据是否合法</span></span><br><span class="line">            <span class="keyword">if</span> form_obj.is_valid():</span><br><span class="line">                <span class="comment"># 5.如果合法 操作数据库存储数据</span></span><br><span class="line">                <span class="keyword">return</span> HttpResponse(<span class="string">&#x27;OK&#x27;</span>)</span><br><span class="line">            <span class="comment"># 5.不合法 有错误</span></span><br><span class="line">        <span class="comment"># 2 直接将该空对象传递给html页面</span></span><br><span class="line">        <span class="keyword">return</span> render(request,<span class="string">&#x27;index.html&#x27;</span>,<span class="built_in">locals</span>())</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">	<span class="comment"># 前端html</span></span><br><span class="line">    &lt;form action=<span class="string">&quot;&quot;</span> method=<span class="string">&quot;post&quot;</span> novalidate&gt;</span><br><span class="line">        &#123;% <span class="keyword">for</span> form <span class="keyword">in</span> form_obj %&#125;</span><br><span class="line">            &lt;p&gt;</span><br><span class="line">                &#123;&#123; form.label &#125;&#125;:&#123;&#123; form &#125;&#125;</span><br><span class="line">                &lt;span style=<span class="string">&quot;color: red&quot;</span>&gt;&#123;&#123; form.errors<span class="number">.0</span> &#125;&#125;&lt;/span&gt;</span><br><span class="line">            &lt;/p&gt;</span><br><span class="line"></span><br><span class="line">        &#123;% endfor %&#125;</span><br><span class="line">        &lt;<span class="built_in">input</span> <span class="built_in">type</span>=<span class="string">&quot;submit&quot;</span> <span class="class"><span class="keyword">class</span>=&quot;<span class="title">btn</span> <span class="title">btn</span>-<span class="title">primary</span> <span class="title">btn</span>-<span class="title">xs</span>&quot;&gt;</span></span><br><span class="line"><span class="class">    &lt;/<span class="title">form</span>&gt;</span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class">&quot;&quot;&quot;</span></span><br><span class="line"><span class="class">1.必备的条件 <span class="title">get</span>请求和<span class="title">post</span>传给<span class="title">html</span>页面对象变量名必须一样</span></span><br><span class="line"><span class="class">2.<span class="title">forms</span>组件当你的数据不合法的情况下 会保存你上次的数据 让你基于之前的结果进行修改</span></span><br><span class="line"><span class="class">更加的人性化</span></span><br><span class="line"><span class="class">&quot;&quot;&quot;</span></span><br><span class="line"><span class="class">    # 针对错误的提示信息还可以自己自定制</span></span><br><span class="line"><span class="class">    <span class="title">class</span> <span class="title">MyForm</span>(<span class="params">forms.Form</span>):</span></span><br><span class="line">        <span class="comment"># username字符串类型最小3位最大8位</span></span><br><span class="line">        username = forms.CharField(min_length=<span class="number">3</span>,max_length=<span class="number">8</span>,label=<span class="string">&#x27;用户名&#x27;</span>,</span><br><span class="line">                                   error_messages=&#123;</span><br><span class="line">                                       <span class="string">&#x27;min_length&#x27;</span>:<span class="string">&#x27;用户名最少3位&#x27;</span>,</span><br><span class="line">                                       <span class="string">&#x27;max_length&#x27;</span>:<span class="string">&#x27;用户名最大8位&#x27;</span>,</span><br><span class="line">                                       <span class="string">&#x27;required&#x27;</span>:<span class="string">&quot;用户名不能为空&quot;</span></span><br><span class="line">                                   &#125;</span><br><span class="line">                                   )</span><br><span class="line">        <span class="comment"># password字符串类型最小3位最大8位</span></span><br><span class="line">        password = forms.CharField(min_length=<span class="number">3</span>,max_length=<span class="number">8</span>,label=<span class="string">&#x27;密码&#x27;</span>,</span><br><span class="line">                                   error_messages=&#123;</span><br><span class="line">                                       <span class="string">&#x27;min_length&#x27;</span>: <span class="string">&#x27;密码最少3位&#x27;</span>,</span><br><span class="line">                                       <span class="string">&#x27;max_length&#x27;</span>: <span class="string">&#x27;密码最大8位&#x27;</span>,</span><br><span class="line">                                       <span class="string">&#x27;required&#x27;</span>: <span class="string">&quot;密码不能为空&quot;</span></span><br><span class="line">                                   &#125;</span><br><span class="line">                                   )</span><br><span class="line">        <span class="comment"># email字段必须符合邮箱格式  xxx@xx.com</span></span><br><span class="line">        email = forms.EmailField(label=<span class="string">&#x27;邮箱&#x27;</span>,</span><br><span class="line">                                 error_messages=&#123;</span><br><span class="line">                                     <span class="string">&#x27;invalid&#x27;</span>:<span class="string">&#x27;邮箱格式不正确&#x27;</span>,</span><br><span class="line">                                     <span class="string">&#x27;required&#x27;</span>: <span class="string">&quot;邮箱不能为空&quot;</span></span><br><span class="line">                                 &#125;</span><br><span class="line">                                 )</span><br></pre></td></tr></table></figure>

<h2 id="六、forms组件钩子函数-HOOK"><a href="#六、forms组件钩子函数-HOOK" class="headerlink" title="六、forms组件钩子函数(HOOK)"></a>六、forms组件钩子函数(HOOK)</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">在特定的节点自动触发完成响应操作</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">钩子函数在forms组件中就类似于第二道关卡，能够让我们自定义校验规则</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">在forms组件中有两类钩子</span></span><br><span class="line"><span class="string">	1.局部钩子</span></span><br><span class="line"><span class="string">		当你需要给单个字段增加校验规则的时候可以使用</span></span><br><span class="line"><span class="string">	2.全局钩子</span></span><br><span class="line"><span class="string">  	当你需要给多个字段增加校验规则的时候可以使用</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 实际案例</span></span><br><span class="line">    <span class="number">1.</span>校验用户名中不能含有<span class="number">666</span>				只是校验username字段  局部钩子</span><br><span class="line">    <span class="number">2.</span>校验密码和确认密码是否一致			password confirm两个字段	全局钩子</span><br><span class="line"></span><br><span class="line"><span class="comment"># 钩子函数  在类里面书写方法即可</span></span><br><span class="line">    <span class="comment"># 局部钩子</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">clean_username</span>(<span class="params">self</span>):</span></span><br><span class="line">        <span class="comment"># 获取到用户名</span></span><br><span class="line">        username = self.cleaned_data.get(<span class="string">&#x27;username&#x27;</span>)</span><br><span class="line">        <span class="keyword">if</span> <span class="string">&#x27;666&#x27;</span> <span class="keyword">in</span> username:</span><br><span class="line">            <span class="comment"># 提示前端展示错误信息</span></span><br><span class="line">            self.add_error(<span class="string">&#x27;username&#x27;</span>,<span class="string">&#x27;光喊666是不行滴～&#x27;</span>)</span><br><span class="line">        <span class="comment"># 将钩子函数钩去出来数据再放回去</span></span><br><span class="line">        <span class="keyword">return</span> username</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 全局钩子</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">clean</span>(<span class="params">self</span>):</span></span><br><span class="line">        password = self.cleaned_data.get(<span class="string">&#x27;password&#x27;</span>)</span><br><span class="line">        confirm_password = self.cleaned_data.get(<span class="string">&#x27;confirm_password&#x27;</span>)</span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> confirm_password == password:</span><br><span class="line">            self.add_error(<span class="string">&#x27;confirm_password&#x27;</span>,<span class="string">&#x27;两次密码不一致&#x27;</span>)</span><br><span class="line">        <span class="comment"># 将钩子函数钩出来数据再放回去</span></span><br><span class="line">        <span class="keyword">return</span> self.cleaned_data</span><br></pre></td></tr></table></figure>

<h2 id="七、重要参数介绍"><a href="#七、重要参数介绍" class="headerlink" title="七、重要参数介绍"></a>七、重要参数介绍</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line">label		字段名</span><br><span class="line">error_messages  自定义报错信息</span><br><span class="line">initial  默认值</span><br><span class="line">required  控制字段是否必填</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">1.字段没有样式</span></span><br><span class="line"><span class="string">2.针对不同类型的input如何修改</span></span><br><span class="line"><span class="string">	text</span></span><br><span class="line"><span class="string">	password</span></span><br><span class="line"><span class="string">	date</span></span><br><span class="line"><span class="string">	radio</span></span><br><span class="line"><span class="string">	checkbox</span></span><br><span class="line"><span class="string">	...</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">	widget=forms.widgets.PasswordInput(attrs=&#123;<span class="string">&#x27;class&#x27;</span>:<span class="string">&#x27;form-control c1 c2&#x27;</span>&#125;)</span><br><span class="line"><span class="comment"># 多个属性值的话 直接空格隔开即可</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 第一道关卡里面还支持正则校验</span></span><br><span class="line">    validators=[</span><br><span class="line">                RegexValidator(<span class="string">r&#x27;^[0-9]+$&#x27;</span>, <span class="string">&#x27;请输入数字&#x27;</span>),</span><br><span class="line">                RegexValidator(<span class="string">r&#x27;^159[0-9]+$&#x27;</span>, <span class="string">&#x27;数字必须以159开头&#x27;</span>)</span><br><span class="line">            ]</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># radio</span></span><br><span class="line">    gender = forms.ChoiceField(</span><br><span class="line">        choices=((<span class="number">1</span>, <span class="string">&quot;男&quot;</span>), (<span class="number">2</span>, <span class="string">&quot;女&quot;</span>), (<span class="number">3</span>, <span class="string">&quot;保密&quot;</span>)),</span><br><span class="line">        label=<span class="string">&quot;性别&quot;</span>,</span><br><span class="line">        initial=<span class="number">3</span>,</span><br><span class="line">        widget=forms.widgets.RadioSelect()</span><br><span class="line">    )</span><br><span class="line"><span class="comment"># select</span></span><br><span class="line">    hobby = forms.ChoiceField(</span><br><span class="line">        choices=((<span class="number">1</span>, <span class="string">&quot;篮球&quot;</span>), (<span class="number">2</span>, <span class="string">&quot;足球&quot;</span>), (<span class="number">3</span>, <span class="string">&quot;双色球&quot;</span>),),</span><br><span class="line">        label=<span class="string">&quot;爱好&quot;</span>,</span><br><span class="line">        initial=<span class="number">3</span>,</span><br><span class="line">        widget=forms.widgets.Select()</span><br><span class="line">    )</span><br><span class="line"><span class="comment"># 多选</span></span><br><span class="line">    hobby1 = forms.MultipleChoiceField(</span><br><span class="line">        choices=((<span class="number">1</span>, <span class="string">&quot;篮球&quot;</span>), (<span class="number">2</span>, <span class="string">&quot;足球&quot;</span>), (<span class="number">3</span>, <span class="string">&quot;双色球&quot;</span>),),</span><br><span class="line">        label=<span class="string">&quot;爱好&quot;</span>,</span><br><span class="line">        initial=[<span class="number">1</span>, <span class="number">3</span>],</span><br><span class="line">        widget=forms.widgets.SelectMultiple()</span><br><span class="line">    )</span><br><span class="line"><span class="comment"># 单选checkbox</span></span><br><span class="line">    keep = forms.ChoiceField(</span><br><span class="line">        label=<span class="string">&quot;是否记住密码&quot;</span>,</span><br><span class="line">        initial=<span class="string">&quot;checked&quot;</span>,</span><br><span class="line">        widget=forms.widgets.CheckboxInput()</span><br><span class="line">    )</span><br><span class="line"><span class="comment"># 多选checkbox</span></span><br><span class="line">    hobby2 = forms.MultipleChoiceField(</span><br><span class="line">        choices=((<span class="number">1</span>, <span class="string">&quot;篮球&quot;</span>), (<span class="number">2</span>, <span class="string">&quot;足球&quot;</span>), (<span class="number">3</span>, <span class="string">&quot;双色球&quot;</span>),),</span><br><span class="line">        label=<span class="string">&quot;爱好&quot;</span>,</span><br><span class="line">        initial=[<span class="number">1</span>, <span class="number">3</span>],</span><br><span class="line">        widget=forms.widgets.CheckboxSelectMultiple()</span><br><span class="line">    )</span><br></pre></td></tr></table></figure>


            </div>

            <!-- Post Comments -->
            
    <!-- 使用 DISQUS_CLICK -->
<div id="disqus-comment">
    <div id="disqus_thread"></div>

<!-- add animation -->
<style>
	.disqus_click_btn {
            line-height: 30px;
            margin: 0;
            min-width: 50px;
            padding: 0 14px;
            display: inline-block;
            font-family: "Roboto", "Helvetica", "Arial", sans-serif;
            font-size: 14px;
            font-weight: 400;
            text-transform: uppercase;
            letter-spacing: 0;
            overflow: hidden;
            will-change: box-shadow;
            transition: box-shadow .2s cubic-bezier(.4, 0, 1, 1), background-color .2s cubic-bezier(.4, 0, .2, 1), color .2s cubic-bezier(.4, 0, .2, 1);
            outline: 0;
            cursor: pointer;
            text-decoration: none;
            text-align: center;
            vertical-align: middle;
            border: 0;
            background: rgba(158, 158, 158, .2);
            box-shadow: 0 2px 2px 0 rgba(0, 0, 0, .14), 0 3px 1px -2px rgba(0, 0, 0, .2), 0 1px 5px 0 rgba(0, 0, 0, .12);
            color: #fff;
            background-color: #7EC0EE;
            text-shadow: 0
        }
</style>
	
<div class="btn_click_load" id="disqus_bt"> 
    <button class="disqus_click_btn">点击查看评论</button>
</div>

<!--
<script type="text/javascript">
$('.btn_click_load').click(function() {
    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
    var disqus_shortname = 'http-miccall-tech'; // required: replace example with your forum shortname

    /* * * DON'T EDIT BELOW THIS LINE * * */
    (function() {
      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();

    document.getElementById('disqus_bt').style.display = "none";
});
</script>
-->
<script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'https://gitee.com/AmeX/amelin-ting.git/2018/05/09/Django------form%E7%BB%84%E4%BB%B6/';  // Replace PAGE_URL with your page's canonical URL variable
        this.page.identifier = 'https://gitee.com/AmeX/amelin-ting.git/2018/05/09/Django------form%E7%BB%84%E4%BB%B6/'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
    };
</script>

<script type="text/javascript">
    $('.btn_click_load').click(function() {  //click to load comments
        (function() { // DON'T EDIT BELOW THIS LINE
            var d = document;
            var s = d.createElement('script');
            s.src = '//http-miccall-tech.disqus.com/embed.js';
            s.setAttribute('data-timestamp', + new Date());
            (d.head || d.body).appendChild(s);
        })();
        $('.btn_click_load').css('display','none');
    });
</script>
</div>
<style>
    #disqus-comment{
        background-color: #eee;
        padding: 2pc;
    }
</style>


        </div>
        <!-- Copyright 版权 start -->
                <div id="copyright">
            <ul>
                <li>&copy;Powered By <a target="_blank" rel="noopener" href="https://hexo.io/zh-cn/" style="border-bottom: none;">hexo</a></li>
                <li>Design: <a target="_blank" rel="noopener" href="http://miccall.tech " style="border-bottom: none;">miccall</a></li>
            </ul>
            
                <span id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
			
        </div>
    </div>
</body>



 	
</html>
